package cn.dengta.webapp.user.entity;

import java.util.*;

import cn.dengta.common.util.Common;
import cn.dengta.common.web.Client;
import cn.dengta.context.model.WesKit;
import cn.dengta.webapp.base.entity.*;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.StringUtil;
import me.codeplayer.util.X;

/**
 * 渠道
 */
@Setter
@Getter
@Accessors(chain = true)
public class Channel extends BizEntity {
	/** 分类标记 */
	protected Long signId;
	/** 渠道 */
	protected String name;
	/** 渠道包名 */
	protected String packageName;
	/** 别名 */
	protected String alias;
	/** 应用id */
	protected String appId;
	/** 排序，数字越大，越靠前 */
	protected Integer sort;
	/** adjust跟踪码, interAd 跟踪码 */
	protected String trackerToken;
	/** 跟踪链接渠道层名字 */
	protected String campaignName;
	/** 旧的跟踪链接渠道层名字 */
	protected String oldCampaignName;
	protected Client client;
	/** 用于上报区分应用 */
	protected String appToken;
	/** app应用 */
	protected String appApply;
	/** 国家（多个逗号隔开） */
	protected String country;
	/** 活动名称，多个逗号隔开 */
	private String activityName;
	/** 渠道所属分包 */
	private WesKit wesKit;

	public Channel init(Channel input, Date now) {
		this.name = X.expectNotNull(name, input.name);
		this.packageName = X.expectNotNull(input.packageName, packageName);
		this.alias = X.expectNotNull(alias, input.alias);
		this.client = X.expectNotNull(input.client, client);
		this.appId = X.expectNotNull(input.appId, appId);
		this.appApply = X.expectNotNull(input.appApply, appApply);
		this.appToken = ChannelPackageName.ofOrDefault(ChannelAppType.of(this.appApply), appId, client).getAlias();
		this.trackerToken = X.expectNotNull(input.trackerToken, trackerToken);
		this.campaignName = X.expectNotNull(input.campaignName, campaignName);
		this.oldCampaignName = X.expectNotNull(input.oldCampaignName, oldCampaignName);
		this.sort = X.expectNotNull(input.sort, sort, 0);
		this.country = StringUtil.toString(input.country);
		this.wesKit = input.wesKit;
		this.signId = X.expectNotNull(input.signId, signId);
		super.init(input.status, input.state);
		super.initTime(now);
		return this;
	}

	public static boolean fromChannel(String packageName) {
		return StringUtil.notEmpty(packageName) && !isStd(packageName);
	}

	public static boolean isStd(String packageName) {
		return "main".equals(packageName);
	}

	public ChannelPackageName checkAppId(ChannelAppType appType, String appId) {
		return ChannelPackageName.ofOrDefault(appType, appId, client);
	}

	public ChannelPackageName checkAppAlias(ChannelAppType appType, String appAlias) {
		return ChannelPackageName.ofAlias(appType, appAlias, client);
	}

	public static boolean checkCampaignName(String campaignName) {
		return StringUtil.notEmpty(campaignName) && X.size(splitCampaignName(campaignName)) >= 5;
	}

	public static List<String> splitCampaignName(String campaignName) {
		return StringUtil.notEmpty(campaignName) ? Common.split(campaignName, '/', String::valueOf, true) : Collections.emptyList();
	}

	public static final String NAME = "name";
	public static final String PACKAGE_NAME = "package_name";
	public static final String CAMPAIGN_NAME = "campaign_name";
	public static final String OLD_CAMPAIGN_NAME = "old_campaign_name";
	public static final String TRACKER_TOKEN = "tracker_token";
	public static final String ALIAS = "alias";
	public static final String APP_TOKEN = "app_token";
	public static final String APP_ID = "app_id";
	public static final String APP_APPLY = "app_apply";
	public static final String SORT = "sort";
	public static final String COUNTRY = "country";
	public static final String CLIENT = "client";

}
